package com.tong.support.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.parser.deserializer.Jdk8DateCodec;
import com.alibaba.fastjson.serializer.*;

import java.util.List;
import java.util.Map;

public class FastJsonUtils {
    private static final SerializeConfig defaultConfig;
    private static final String FMT_DEFAULT = "yyyy-MM-dd HH:mm:ss";

    static {
        defaultConfig = new SerializeConfig();
        defaultConfig.put(java.util.Date.class, new Jdk8DateCodec());
        defaultConfig.put(java.sql.Date.class, new Jdk8DateCodec());
    }

    private static final SerializerFeature[] defaultFeatures = {
            SerializerFeature.WriteMapNullValue,
            SerializerFeature.WriteNullListAsEmpty,
            SerializerFeature.WriteNullBooleanAsFalse,
            SerializerFeature.WriteNullStringAsEmpty,
    };

    public static String serializer(Object obj) {
        return JSON.toJSONString(obj, defaultConfig, defaultFeatures);
    }

    public static String serializer(Object obj, SerializerFeature... features) {
        return JSON.toJSONString(obj, defaultConfig, features);
    }

    public static String serializer(Object obj, SerializeFilter filter) {
        return JSON.toJSONString(obj, defaultConfig, filter);
    }

    public static String serializer(Object obj, SerializeFilter filter, SerializerFeature... features) {
        return JSON.toJSONString(obj, defaultConfig, filter, features);
    }

    public static <T> T deserializer(String json, TypeReference<T> ref) {
        return JSON.parseObject(json, ref);
    }

    public static <T> T deserializer(String json, Class<T> clazz) {
        return JSON.parseObject(json, clazz);
    }

    public static Object toObject(String json) {
        return JSON.parse(json);
    }

    public static <T> Object[] toArray(String json) {
        return toArray(json);
    }

    public static <T> Object[] toArray(String json, Class<T> clazz) {
        return JSON.parseArray(json, clazz).toArray();
    }

    public static <T> List<T> toList(String json, Class<T> clazz) {
        return JSON.parseArray(json, clazz);
    }

    public static Map toMap(String json) {
        Map map = JSONObject.parseObject(json);
        return map;
    }


}
